package top.oylan.manager.controller.admin;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import top.oylan.manager.entity.Role;
import top.oylan.manager.entity.RoleUser;
import top.oylan.manager.entity.User;
import top.oylan.manager.service.RoleService;
import top.oylan.manager.service.UserService;
import top.oylan.manager.tools.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author abduahad
 * @Description ${description}
 * @Date 2019-04-20
 */
@Controller
@RequestMapping("/admin/manager")
public class AdminManagerController {

    @Resource
    UserService userService;

    @RequestMapping("/list")
    public String index(ModelMap modelMap, @RequestParam(value = "page",defaultValue = "1")Integer page){

        Pager<User> pager = new Pager<User>();

        User condition = new User();
        condition.setType((byte)1);

        pager.setCondition(condition);
        pager.setCurrentPage(page);

        pager.setList(userService.selectManagerByPager(pager));

        modelMap.addAttribute("pager",pager);
        return "admin/manager/list";
    }

    @RequestMapping("/add")
    public String add(ModelMap modelMap){

        List<Role> role = getRoles();
        modelMap.addAttribute("roleList",role);
        return "admin/manager/add";
    }

    @RequestMapping("/edit")
    public String edit(ModelMap modelMap,@RequestParam(value = "id",defaultValue = "0")Integer id){

        User user = userService.selectUserById(id);
        modelMap.addAttribute("user",user);

        List<Role> role = getRoles();
        modelMap.addAttribute("roleList",role);

        List<RoleUser> userRoles = roleService.getUserRoles(id);
        List<Integer> roleIds = new ArrayList<>();
        for (RoleUser userRole:userRoles){
            Integer roleId = userRole.getRoleId();
            roleIds.add(roleId);
        }
        modelMap.addAttribute("roleIds",roleIds);
        return "admin/manager/edit";
    }

    @Resource
    RoleService roleService;
    private List<Role> getRoles(){
        return roleService.selectAll();
    }


    @ResponseBody
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public Result save(@RequestBody JSONObject object){
        Result result = new Result();
        JSONArray ids = object.getJSONArray("roles");
        User user = object.getObject("user", User.class);
        List<Integer> idsList = new ArrayList<>();
        for (int i=0;i<ids.size();i++){
            idsList.add(ids.getIntValue(i));
        }
        if(user.getId() != null && user.getId() > 0){
            userService.updateManger(user,idsList);
        }else{
            userService.addManger(user,idsList);
        }
        result.setCode((byte) 1);
        return result;
    }

    @Resource
    FileUploader fileUploader;

    @RequestMapping(value = "/upload",method = RequestMethod.POST)
    @ResponseBody
    public JSONObject upload(@RequestParam("file") MultipartFile file){
        JSONObject object = new JSONObject();
        Result res = fileUploader.upload(file, Directory.IMAGE);
        if(res.getCode() == 1){
            object.put("path",res.getData().get("path").toString());
            object.put("status",1);
        }else{
            object.put("status",0);
        }
        return object;
    }


    @RequestMapping(value = "/file/remove",method = RequestMethod.GET)
    public Result unlink(@RequestParam(value = "path")String path){

        Result result = new Result((byte) 1,"成功");
        fileUploader.removeFile(SystemConstants.UPLOAD_ROOT_PATH+path);
        return result;
    }

}
